package com.project.controller;

import com.project.bean.AddMoney;
import com.project.bean.ExpendRecord;
import com.project.bean.UserBean;
import com.project.dto.UpdateDiscountDto;
import com.project.service.IAddMoneyService;
import com.project.service.IExpendService;
import com.project.service.IUserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("money")
public class AddMoneyController {
      @Autowired
      RabbitTemplate rabbitTemplate;
      @Autowired
      private IAddMoneyService service;
      @Autowired
      private IExpendService expendService;
      @Autowired
      private IUserService userService;
      @Autowired
      private com.project.webOneService.IAddMoneyService iAddMoneyService;
      @RequestMapping("addMoney")
      public String addMoney(double price, HttpServletRequest request) {
            AddMoney addMoney=new AddMoney();
            addMoney.setMoney(price);
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            addMoney.setUserNum(userBean.getUserNum());
            //调用审核的feign,返回用户对象
            UpdateDiscountDto updateDiscountDto=iAddMoneyService.updateUserDiscount(addMoney.getUserNum(),addMoney.getMoney());
            //修改用户的折扣率
            userService.updateDiscount(updateDiscountDto.getUserNum(),updateDiscountDto.getDiscount(),updateDiscountDto.getTotalPrice());
            //加钱
            service.addMoney(addMoney);
            /*String jsonString = JSONObject.toJSONString(addMoney);
            rabbitTemplate.convertAndSend("webOne","addMoney",jsonString);*/
            return "ok";
      }
      @RequestMapping("findAll")
      public List<AddMoney> findAll(HttpServletRequest request) {
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return service.findAll(userBean.getUserNum());
      }
      @RequestMapping("findExpend")
      public List<ExpendRecord> findExpend(HttpServletRequest request) {
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return expendService.findAll(userBean.getUserNum());
      }
}
